package com.mcr.graduateprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.graduateprogram.bean.GameSpecification;
import com.mcr.graduateprogram.bean.Games;
import com.mcr.graduateprogram.bean.Manager;
import com.mcr.graduateprogram.mapper.GameSpecificationMapper;
import com.mcr.graduateprogram.mapper.GamesMapper;
import com.mcr.graduateprogram.service.IGamesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author graduateprogram
 * @since 2023-02-25
 */
@Service
public class GamesServiceImpl extends ServiceImpl<GamesMapper, Games> implements IGamesService {
    @Autowired
    private GamesMapper gamesMapper;
    @Autowired
    private GameSpecificationMapper gameSpecificationMapper;
    @Override
    public Page<Games> getPage(Integer page, Integer pageSize, Long classesId) {
        Page<Games> gamesPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Games> gamesLambdaQueryWrapper=new LambdaQueryWrapper<>();
        gamesLambdaQueryWrapper.eq(classesId!=null,Games::getClassesId,classesId);
        gamesMapper.selectPage(gamesPage,gamesLambdaQueryWrapper);
        return gamesPage;
    }

    @Override
    public Boolean delById(Long id) {
        LambdaQueryWrapper<GameSpecification> gameSpecificationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        gameSpecificationLambdaQueryWrapper.eq(GameSpecification::getGameId,id);
        List<GameSpecification> gameSpecificationList = gameSpecificationMapper.selectList(gameSpecificationLambdaQueryWrapper);
        boolean t=true;
        for(int i=0;i<gameSpecificationList.size();i++){
            if(gameSpecificationList.get(i).isPutaway()){
                t=false;//存在一个版本的该游戏还处于上架状态
                break;
            }
        }
        if(t){//该游戏下所有规格全都下架了，可以删
            gamesMapper.deleteById(id);
        }
        return t;
    }

    @Override
    public Boolean deByIds(List<Long> ids) {
        LambdaQueryWrapper<GameSpecification> gameSpecificationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        gameSpecificationLambdaQueryWrapper.in(GameSpecification::getGameId,ids);
        List<GameSpecification> gameSpecifications = gameSpecificationMapper.selectList(gameSpecificationLambdaQueryWrapper);
        boolean t=true;
        for(int i=0;i<gameSpecifications.size();i++){
            if(gameSpecifications.get(i).isPutaway()){
                t=false;//存在一个版本的某游戏还处于上架状态
                break;
            }
        }
        if(t){//这些游戏的所有规格全都下架了，可以删
            gamesMapper.deleteBatchIds(ids);
        }
        return t;
    }
}
